package com.brillilab.starter.controller.aio.method;

import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.enums.method.MethodSearchTypeEnum;
import com.brillilab.domain.pa.method.MethodListPa;
import com.brillilab.domain.po.lab.LabMemberPower;
import com.brillilab.domain.vo.method.*;
import com.brillilab.domain.vo.topics.NewMethodTypeVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.method.IMethodRecommendAuditService;
import com.brillilab.service.core.method.IMethodSearchService;
import com.brillilab.service.core.method.IMethodService;
import com.brillilab.service.core.method.IMethodShareService;
import com.brillilab.service.logic.method.MethodTypeLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 方法库首页
 * 
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/method/index")
public class MethodIndexController {

	@Autowired
	private MethodTypeLogic methodTypeLogic;
	@Autowired
	private ILabMemberService labMemberService;
	@Autowired
	private UsersLogic usersLogic;
	@Autowired
	private IMethodService methodService;
	@Autowired
	private IMethodSearchService methodSearchService;
	@Autowired
	private IMethodRecommendAuditService methodRecommendAuditService;
	@Autowired
	private IMethodShareService methodShareService;

	/**
	 * 获取管理方法类型下成员列表
	 * 
	 * @param token
	 * @param state 状态:1-正常，2-移出,null-全部
	 * @return
	 */
	@GetMapping("/memberList")
	public ResponseVo memberList(Integer state, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		// 查询所有
		List<MethodMemberVo> memberList = methodService.getMethodMemberList(userVo.getId(),userVo.getLabId(), state);
		return ResponseVo.success(memberList);
	}

	@GetMapping("/outMemberList")
	public ResponseVo outMemberList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		// 查询所有
		List<MethodMemberVo> memberList = methodService.getMethodMemberList(userVo.getId(),userVo.getLabId(), 2);
		List<MethodMemberListVo> voList = new ArrayList<>();
		Map<String, List<MethodMemberVo>> map = memberList.stream()
				.collect(Collectors.groupingBy(MethodMemberVo::getOutTime));
		for (String outTime : map.keySet()) {
			MethodMemberListVo vo = new MethodMemberListVo();
			vo.setOutTime(outTime);
			vo.setMemberList(map.get(outTime));
			voList.add(vo);
		}
		if (voList.size() > 1) {
			Collections.sort(voList, new Comparator<MethodMemberListVo>() {
				@Override
				public int compare(MethodMemberListVo o1, MethodMemberListVo o2) {
					return o2.getOutTime().compareTo(o1.getOutTime());
				}
			});
		}
		return ResponseVo.success(voList);
	}

	/**
	 * 获取用户三级类型
	 * 
	 * @param userId
	 * @param token
	 * @return
	 */
	@GetMapping("/thirdType")
	public ResponseVo getThirdType(Long userId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(userId, "参数缺失");
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		boolean isOwner = labMemberService.isOwner(userVo.getLabId(), userVo.getLabMemberId());
		if (!isOwner && !userVo.getId().equals(userId)) {
			Assert.isTrue(!CollectionUtils.isEmpty(userVo.getPowers()), "无实验室方法管理");
			List<String> powerList = userVo.getPowers().stream().map(LabMemberPower::getPower)
					.collect(Collectors.toList());
			boolean contains = ArrayUtils.contains(powerList.toArray(), LabMemberPowerEnum.Power.METHOD.getValue());
			Assert.isTrue(contains, "无实验室方法管理");
		}
		List<NewMethodTypeVo> rs = methodTypeLogic.getAllVisibleMethodTypeMap(userId);
//		methodTypeService.getThirdTypeList(userVo.getLabId(), userId, userVo.getId().equals(userId) ? 1 : 0)
		return ResponseVo.success(rs);
	}

	/**
	 * 获取用户方法列表(分页)
	 * 
	 * @param pa
	 * @param token
	 * @return
	 */
	@PostMapping("/list")
	public ResponseVo methodList(@RequestBody MethodListPa pa,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.isTrue(pa != null && pa.getUserId() != null, "参数缺失");
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		pa.setLabId(userVo.getLabId());
		if (StringUtils.isNotBlank(pa.getMethodName())) {
			methodSearchService.insertMethodSearchRecord(pa.getMethodName(),
					MethodSearchTypeEnum.METHOD_REPOSITORY.getType(), userVo.getId());
		}
		PageVo<MethodListVo> page = methodService.getMethodList(pa);
		return ResponseVo.success(page);
	}

	/**
	 * 参考方法统计数据
	 * 
	 * @return
	 */
	@GetMapping("/referData")
	public ResponseVo referenceData() {
		ReferMethodStatisticsVo vo = methodService.referMethodStatistics();
		return ResponseVo.success(vo);
	}

	/**
	 * 获取推荐参考方法(为你推荐)
	 * 
	 * @return
	 */
	@GetMapping("/referRecommend")
	public ResponseVo referRecommend(Long userId, Long methodTypeId) {
		Assert.notNull(userId, "参数缺失");
		List<MethodReferRecommendVo> voList = methodService.getMethodReferRecommendList(userId, methodTypeId);
		return ResponseVo.success(voList);
	}

	/**
	 * 待审核和分享未处理条数
	 * 
	 * @param token
	 * @return
	 */
	@GetMapping("/cueData")
	public ResponseVo cueData(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		int auditNum = methodRecommendAuditService.countUntreated(userVo.getLabId());
		int shareNum = methodShareService.countUntreated(userVo.getLabMemberId());
		Map<String, Object> map = new HashMap<>();
		map.put("auditNum", auditNum);
		map.put("shareNum", shareNum);
		return ResponseVo.success(map);
	}

}
